package cn.xopencode.recordservice.service.meterroom;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.IdUtil;
import cn.xopencode.common.framework.exception.util.ServiceExceptionUtil;
import cn.xopencode.common.framework.util.DateUtil;
import cn.xopencode.common.framework.vo.PageResult;
import cn.xopencode.recordservice.convert.meterroom.MeterRoomRecordConvert;
import cn.xopencode.recordservice.convert.robotroom.AdminRecordConvert;
import cn.xopencode.recordservice.dal.mysql.dataobject.meterroom.MeterRoomRecordDO;
import cn.xopencode.recordservice.dal.mysql.dataobject.meterroom.MeterRoomRecordEmpRelDO;
import cn.xopencode.recordservice.dal.mysql.dataobject.pdroom.PowerDistributionRoomDO;
import cn.xopencode.recordservice.dal.mysql.mapper.meterroom.MeterRoomRecordEmpRelMapper;
import cn.xopencode.recordservice.dal.mysql.mapper.meterroom.MeterRoomRecordMapper;
import cn.xopencode.recordservice.dal.mysql.mapper.pdroom.PowerDistributionRoomMapper;
import cn.xopencode.recordservice.rpc.meterroom.vo.MeterRoomRecordAdminPageVO;
import cn.xopencode.recordservice.rpc.meterroom.vo.MeterRoomRecordPageVO;
import cn.xopencode.recordservice.rpc.meterroom.vo.MeterRoomRecordUpdateVO;
import cn.xopencode.recordservice.service.meterroom.bo.MeterRoomRecordCreateBO;
import cn.xopencode.recordservice.service.meterroom.bo.MeterRoomRecordPageBO;
import cn.xopencode.recordservice.service.meterroom.bo.MeterRoomRecordUpdateBO;
import cn.xopencode.recordservice.service.robotroom.bo.AdminRecordPageBO;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;

import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

import static cn.xopencode.recordservice.enums.RecordErrorCodeConstants.RECORD_ROBOT_ROOM_NOT_EXCEPTION;
import static cn.xopencode.recordservice.enums.RecordErrorCodeConstants.RECORD_ROBOT_ROOM_NOT_EXITS;

@Service
@Validated
public class MeterRoomRecordService {
    @Autowired
    private MeterRoomRecordMapper roomRecordMapper;
    @Autowired
    private MeterRoomRecordEmpRelMapper roomRecordEmpRelMapper;
    @Autowired
    private PowerDistributionRoomMapper powerDistributionRoomMapper;

    /**
     * 分页查询记录
     * @param pageBO
     * @return
     */
    public PageResult<MeterRoomRecordPageVO> pageMeterRoomRecord(MeterRoomRecordPageBO pageBO){
        IPage<MeterRoomRecordDO> recordDOIPage = roomRecordMapper.selectPage(pageBO);
        PageResult<MeterRoomRecordPageVO> recordPageVOPageResult = MeterRoomRecordConvert.INSTANCE.convert(recordDOIPage);
        if (CollectionUtil.isNotEmpty(recordPageVOPageResult.getList())){
            List<MeterRoomRecordPageVO> list = recordPageVOPageResult.getList();
            for (MeterRoomRecordPageVO vo : list) {
                List<MeterRoomRecordEmpRelDO> empRelDOList = roomRecordEmpRelMapper.selectListByRecordId(vo.getId());
                List<String> tmp = empRelDOList.stream().map(MeterRoomRecordEmpRelDO::getEmpId).collect(Collectors.toList());
                vo.setEmpIds(String.join(",", tmp));
            }
        }
        return recordPageVOPageResult;
    }

    /**
     * 添加记录
     * @param createBO
     * @param empIds
     * @return
     */
    public Integer createMeterRoomRecord(MeterRoomRecordCreateBO createBO, Set<String> empIds){
        MeterRoomRecordDO recordDO = MeterRoomRecordConvert.INSTANCE.convert(createBO);
        String recordId = IdUtil.objectId();
        recordDO.setId(recordId);
        Integer result = roomRecordMapper.insert(recordDO);
        if (!CollectionUtil.isEmpty(empIds)){
            List<MeterRoomRecordEmpRelDO> empRelDOList = empIds.stream()
                    .map(empId -> new MeterRoomRecordEmpRelDO().setId(IdUtil.objectId()).setEmpId(empId).setRecordId(recordId)).collect(Collectors.toList());
            roomRecordEmpRelMapper.insertList(empRelDOList);
        }
        return result;
    }

    /**
     * 更新记录
     * @param updateBO
     * @param empIds
     * @return
     */
    public Integer updateMeterRoomRecord(MeterRoomRecordUpdateBO updateBO,Set<String> empIds){
        MeterRoomRecordDO roomRecordDO = MeterRoomRecordConvert.INSTANCE.convert(updateBO);
        Integer result = roomRecordMapper.updateById(roomRecordDO);
        if (result ==null || result != 1){
            throw ServiceExceptionUtil.exception(RECORD_ROBOT_ROOM_NOT_EXITS);
        }
        roomRecordEmpRelMapper.deleteByRecordId(roomRecordDO.getId());

        if (!CollectionUtil.isEmpty(empIds)){
            List<MeterRoomRecordEmpRelDO> empRelDOList = empIds.stream()
                    .map(empId -> new MeterRoomRecordEmpRelDO().setId(IdUtil.objectId()).setEmpId(empId).setRecordId(roomRecordDO.getId())).collect(Collectors.toList());
            roomRecordEmpRelMapper.insertList(empRelDOList);
        }
        return result;
    }

    /**
     * 通过id查询对应记录
     * @param id
     * @return
     */
    public MeterRoomRecordUpdateVO getMeterRoomRecordById(String id){
        MeterRoomRecordDO recordDO = roomRecordMapper.selectById(id);
        if (recordDO != null){
            MeterRoomRecordUpdateVO updateVO = MeterRoomRecordConvert.INSTANCE.convert(recordDO);

            List<MeterRoomRecordEmpRelDO> empRelDOList = roomRecordEmpRelMapper.selectListByRecordId(id);
            updateVO.setEmpList(MeterRoomRecordConvert.INSTANCE.convert(empRelDOList));
            PowerDistributionRoomDO roomDO = powerDistributionRoomMapper.selectById(recordDO.getRoomId());
            if(roomDO != null)
                updateVO.setRoomName(roomDO.getRoomName());
            return updateVO;
        }
        return null;
    }

    /**
     * 处理异常
     * @param recordId
     * @return
     */
    public Integer dealMeterRoomRecordException(String recordId){
        MeterRoomRecordDO recordDO = roomRecordMapper.selectById(recordId);
        if(recordDO == null){
            throw ServiceExceptionUtil.exception(RECORD_ROBOT_ROOM_NOT_EXITS);
        }
        if(recordDO != null && recordDO.getNoticeLeader() != true){
            throw ServiceExceptionUtil.exception(RECORD_ROBOT_ROOM_NOT_EXCEPTION);
        }
        recordDO.setLeaderDeal(true);
        return roomRecordMapper.updateById(recordDO);
    }

    /**
     * 后台：根据查询条件分页获取仪表机柜间巡检记录
     * @param pageBO
     * @return
     */
    public PageResult<MeterRoomRecordAdminPageVO> getAdminPageMeterRoomRecord(AdminRecordPageBO pageBO){
        Page<MeterRoomRecordDO> page = new Page<>(pageBO.getPageNo(),pageBO.getPageSize());
        if (pageBO.getInspectionTime() != null){
            String date = DateUtil.format(pageBO.getInspectionTime(), "yyyy-MM-dd");
            pageBO.setInspectionTime1(date.concat("00:00:00"));
            pageBO.setInspectionTime2(date.concat("12:59:59"));
        }
        IPage<MeterRoomRecordDO> recordDOList = roomRecordMapper.adminQuery(page, pageBO);
        return AdminRecordConvert.INSTANCE.convert8(recordDOList);
    }
}
